﻿using System.Linq.Expressions;
using MG.Library.Models;

namespace MG.Library.Pages;

public partial class Result
{
    private Expression<Func<Mg, bool>> _where = p => true;

    public const string Loading = "正在载入";

    public const string NoResult = "没有满足条件的结果";

    public const string NoMoreResult = "没有更多结果";

    private string _status = string.Empty;

    public const int PageSize = 20;

    private List<Mg> _mgs = new();

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender)
        {
            return;
        }

        //TODO 测试代码
        await LoadMoreAsync();
    }

    private async Task LoadMoreAsync()
    {
        _status = Loading;
        var mgs=
        await _mgStorage.GetMgsAsync(_where, 
            _mgs.Count, PageSize);
        _status = string.Empty;
        _mgs.AddRange(mgs);

        if (_mgs.Count() < PageSize)
        {
            _status = NoMoreResult;
        }

        if (!_mgs.Any() && _mgs.Count == 0)
        {
            _status = NoResult;
        }
    }
}